package light.head.controller.inv;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.inv.InvAccountDAO;
import light.head.dao.inv.InvBillConfingDAO;
import light.head.dao.inv.InvBusinessDAO;
import light.head.dao.inv.InvReceiptsDAO;
import light.head.dao.inv.InvSettlementDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.inv.InvReceipts;
import light.head.model.inv.InvSettlement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = Url.C_INVRECEIPTS)
public class InvReceiptsController extends SupporterController{
	private InvReceiptsDAO irDAO;
	private OrganDAO organDAO;
	private InvSettlementDAO isDAO;
	private InvAccountDAO iaDAO;
	private InvBusinessDAO ibDAO;
	private InvBillConfingDAO ibcfDAO;
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.INVRECEIPTS_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.ON, Parm.CN, Parm.TIME1, Parm.TIME2, Parm.DELETED,Parm.DN);
		if (irDAO.findByMap(organDAO.getSonCodeList(cu_organCode(request)), map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.INVRECEIPTS_LIST);
	}
	
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if (null != code) {
			InvReceipts ir = irDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.INVRECEIPTS, ir);
		} 
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		List<InvSettlement> typeList = isDAO.findAll(oc);
		if (null != typeList && typeList.size() > 0) {
			StringBuffer result = new StringBuffer("[");
			
			for (InvSettlement type : typeList){
				result.append("{");
				result.append("\"sn\":\"" + type.getName() +"\",");
				result.append("\"sc\":\"" + type.getCode() +"\"");
				result.append("},");
			}
			result.deleteCharAt(result.length() - 1).append("]");
			request.setAttribute(Parm.TYPE, result.toString());
		} 
		return new ModelAndView(Url.INVRECEIPTS_EDIT);
	}
	
	@RequestMapping(value = Url.SAVE)
	public void save(InvReceipts ir, HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (ir == null) {
			print(response, Msg.PARAMETER_ERROR);
			return ;
		}
		RESULT result = RESULT.ERROR;
		if (ir.getCode() == null) {
			Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			List<InvSettlement> isc = isDAO.findByNameOc(ir.getSn(), oc);
			if (isc == null || isc.size() == 0)  {
				InvSettlement is = new InvSettlement();
				is.setName(ir.getSn());
				is.setCt(new Date());
				is.setDeleted(false);
				is.setOc(oc);
				is.setCu(cu_code(request));
				isDAO.add(is);
				ir.setSc(is.getCode());
			}
			if (ir.getType() == 1) {
				ir.setDn(ibcfDAO.createBCode(organDAO.getRoot(cu_organCode(request)).getCode(), Sys.B_FIVE));
			} else if(ir.getType() == 2) {
				ir.setDn(ibcfDAO.createBCode(organDAO.getRoot(cu_organCode(request)).getCode(), Sys.B_SIX));
			}
			ir.setOc(cu_organCode(request));
			ir.setCu(cu_code(request));
			ir.setCt(new Date());
			ir.setDeleted(false);
			result = irDAO.add(ir);
		} else {
			result = irDAO.upd(ir);
		} if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvReceipts ir = irDAO.get(Integer.parseInt(code));
		if (null == ir) {
			print(response, Msg.NOT_FIND);
			return;
		}
		ir.setDeleted(true);
		if (irDAO.upd(ir) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvReceipts ir = irDAO.get(Integer.parseInt(code));
		if (null == ir) {
			print(response, Msg.NOT_FIND);
			return;
		}
		ir.setDeleted(false);
		if (irDAO.upd(ir) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	/**
	 * 查询来往商
	 */
	@RequestMapping(Url.AID_INIT)
	public ModelAndView aidInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.INVRECEIPTS_AID_INIT);
	}
	
	@RequestMapping(Url.AID_LIST)
	public ModelAndView aidList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.OC, Parm.NAME);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (RESULT.SUCCESS == ibDAO.findByMap(oc, map, pageBean)) {
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		}
		return new ModelAndView(Url.INVRECEIPTS_AID_LIST);
	}
	/**
	 * 查询结算方式
	 */
	@RequestMapping(Url.AID_INIT_1)
	public ModelAndView aidInit1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.INVRECEIPtS_AID_INIT1);
	}
	
	@RequestMapping(Url.AID_LIST_1)
	public ModelAndView aidList1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.OC, Parm.NAME);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (RESULT.SUCCESS == isDAO.findByMap(oc, map, pageBean)) {
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		}
		return new ModelAndView(Url.INVRECEIPTS_AID_LIST1);
	}
	/**
	 * 查询账号
	 */
	@RequestMapping(Url.AID_INIT_2)
	public ModelAndView aidInit2(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.INVRECEIPTS_AID_INIT2);
	}
	
	@RequestMapping(Url.AID_LIST_2)
	public ModelAndView aidList2(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.OC, Parm.NAME);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (RESULT.SUCCESS == iaDAO.findByMap(oc, map, pageBean)) {
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		}
		return new ModelAndView(Url.INVRECEIPTS_AID_LIST2);
	}
	@Autowired
	public void setIrDAO(InvReceiptsDAO irDAO) {
		this.irDAO = irDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setIsDAO(InvSettlementDAO isDAO) {
		this.isDAO = isDAO;
	}
	@Autowired
	public void setIaDAO(InvAccountDAO iaDAO) {
		this.iaDAO = iaDAO;
	}
	@Autowired
	public void setIbDAO(InvBusinessDAO ibDAO) {
		this.ibDAO = ibDAO;
	}
	@Autowired
	public void setIbcfDAO(InvBillConfingDAO ibcfDAO) {
		this.ibcfDAO = ibcfDAO;
	}
	
}
